package controllers

import (
	"github.com/astaxie/beego"
	"choujiang/models"
	"HTchoujiang/htmodels"
	"strconv"
	"time"
	"strings"
	"github.com/astaxie/beego/validation"
)

type DevicesCouponController struct {
	beego.Controller
}

//带了参数导致bootstrap table无法正常使用
func (c *DevicesCouponController)HTDevicesCouponList_html() {
	str_CouponStatus := c.Ctx.Input.URL()[strings.LastIndex(c.Ctx.Input.URL(), "/") + 1:]
	CouponStatus, _ := strconv.Atoi(str_CouponStatus)
	c.Data["CouponStatus"] = CouponStatus
	c.TplName = "devicescoupon_list.html"
	c.Render()
}

func (c *DevicesCouponController)HTDevicesCouponList() {
	CouponStatus, _ := c.GetInt("CouponStatus")
	limit, _ := c.GetInt("limit")
	offset, _ := c.GetInt64("offset")
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{ "Id", "CouponName", "CouponDiscountLeastPrice", "CouponDiscountPrice", "CouponMessage", "CouponPublishTime", "CouponEndTime"}
	DevicesCoupon := []models.DevicesCoupon{}
	htmodels.HTDevicesCouponGetList(&DevicesCoupon, fileds, CouponStatus,  offset, limit, search, "CouponName", orderby)
	count, _ := htmodels.HTDevicesCouponGetCount(search, "CouponName", CouponStatus, )
	c.Data["json"] = map[string]interface{}{"rows":&DevicesCoupon, "total":count}
	c.ServeJSON()
}


func (c *DevicesCouponController)HTDevicesCouponDetail() {
	Id, _ := c.GetInt64("Id")
	if Id != 0 {
		c.Data["update"] = true
		SetDevicesCouponDetailInfo(c, Id)
	}
	mytype,_:=c.GetInt("type")
	c.Data["Type"]=mytype
	c.TplName = "devicescoupon_detail.html"
}

func SetDevicesCouponDetailInfo(c *DevicesCouponController, Id int64) {
	DevicesCoupon := models.DevicesCoupon{Id:int(Id)}
	htmodels.HTDevicesCouponGetDetail(&DevicesCoupon)
	c.Data["DevicesCoupon"] = &DevicesCoupon
}

func (c *DevicesCouponController)HTDevicesCouponAdd() {
	DevicesCoupon := models.DevicesCoupon{}
	if err := c.ParseForm(&DevicesCoupon); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	if time_up, err := time.ParseInLocation("2006-01-02 15:04:05", c.GetString("couponpublishtime"),time.Local);err!=nil{
		DevicesCoupon.CouponPublishTime=time.Now()
	}else{
		DevicesCoupon.CouponPublishTime=time_up
	}

	if time_up, err := time.ParseInLocation("2006-01-02 15:04:05", c.GetString("couponendtime"),time.Local);err!=nil{
		DevicesCoupon.CouponEndTime=time.Now()
	}else{
		DevicesCoupon.CouponEndTime=time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&DevicesCoupon); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTDevicesCouponAdd(&DevicesCoupon); err != nil {
		Message(&c.Controller, "", "添加失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/devicescoupon/detail`, "添加成功！", "", false)
}


func (c *DevicesCouponController)HTDevicesCouponUpdate() {
	Id , _ := c.GetInt64("Id");
	DevicesCoupon := models.DevicesCoupon{Id:int(Id)}
	if err:=htmodels.HTDevicesCouponGetDetail(&DevicesCoupon);err!=nil{
		Message(&c.Controller, "", "严重错误，请联系管理员,即将返回！" + err.Error(), "", true)
	}
	if err := c.ParseForm(&DevicesCoupon); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	if time_up, err := time.ParseInLocation("2006-01-02 15:04:05", c.GetString("couponpublishtime"),time.Local);err!=nil{
		DevicesCoupon.CouponPublishTime=time.Now()
	}else{
		DevicesCoupon.CouponPublishTime=time_up
	}

	if time_up, err := time.ParseInLocation("2006-01-02 15:04:05", c.GetString("couponendtime"),time.Local);err!=nil{
		DevicesCoupon.CouponEndTime=time.Now()
	}else{
		DevicesCoupon.CouponEndTime=time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&DevicesCoupon); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTDevicesCouponUpdate(&DevicesCoupon); err != nil {
		Message(&c.Controller, "", "更新失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/devicescoupon/list_html`+`/`+strconv.Itoa(DevicesCoupon.CouponStatus), "更新成功！", "", false)
}


func (c *DevicesCouponController)HTDevicesCouponDelete() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	count, err := htmodels.HTDevicesCouponDelete(Ids)
	if err != nil {
		Json_Message(&c.Controller, 0, "删除失败" + err.Error())
	}
	Json_Message(&c.Controller, 1, "删除" + strconv.FormatInt(count, 10) + "行")
}


func (c *DevicesCouponController)HTDevicesCouponStatusChange() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	CouponStatus, err1 := c.GetInt("ChangedStatus")
	if err1 != nil {
		Json_Message(&c.Controller, 0, "更改失败")
	}
	count, err := htmodels.HTDevicesCouponStatusChange(Ids, CouponStatus)
	if err != nil {
		Json_Message(&c.Controller, 0, "更改失败" + err.Error())
	}
	Json_Message(&c.Controller, 1, "更改" + strconv.FormatInt(count, 10) + "行状态")
}

